package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.entity.IncomeLog;
import com.sise.parkinglot.mapper.IncomeLogMapper;
import com.sise.parkinglot.service.IIncomeLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class IncomeLogServiceImpl extends ServiceImpl<IncomeLogMapper, IncomeLog> implements IIncomeLogService {

    @Resource
    private IncomeLogMapper incomeLogMapper;

    @Override
    public CodeMsg<List<IncomeLog>> queryIncomeLogList(Map<String, Object> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<IncomeLog>>().setCode(400).setMsg("参数缺失");
        }
        Page<IncomeLog> page = new Page<>((int) map.get("current"), (int) map.get("pageSize"));
        QueryWrapper<IncomeLog> incomeLogQueryWrapper = new QueryWrapper<>();
        if (map.get("id") != null && !map.get("id").equals("")) {
            incomeLogQueryWrapper.eq("id", map.get("id"));
        }
        if (map.get("income") != null && !map.get("income").equals("")) {
            incomeLogQueryWrapper.like("income", map.get("income"));
        }
        if (map.get("recordDate") != null && !map.get("recordDate").equals("")) {
            incomeLogQueryWrapper.eq("record_date", map.get("recordDate"));
        }
        incomeLogQueryWrapper.lambda().orderByDesc(IncomeLog::getRecordDate);
        IPage<IncomeLog> incomeLogPage = incomeLogMapper.selectPage(page, incomeLogQueryWrapper);
        List<IncomeLog> incomeLogList = incomeLogPage.getRecords();
        return new CodeMsg<List<IncomeLog>>().setCode(200).setMsg(String.valueOf(incomeLogPage.getTotal())).setData(incomeLogList);
    }
}
